
//
//  CustomTransitionAnimation.m
//  自定义转场动画
//
//  Created by MyMac on 15/8/18.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "CustomTransitionAnimation.h"
#import "ViewController.h"
#import "BaseViewController.h"
@interface CustomTransitionAnimation()
@property(nonatomic,weak)id<UIViewControllerContextTransitioning>transitionContext;
@end
@implementation CustomTransitionAnimation
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext;
{
    if (self.duration) {
        return self.duration;
    }else{
        return 1.0;
    }
    

}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
{
    self.transitionContext=transitionContext;
    ViewController*fromVC=(ViewController*)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    BaseViewController*toVC=(BaseViewController*)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    //toVC的view在fromVC的view的上面，所以会看到toVC的动画完成后，fromVC的动画看不见了
    //转场动画必须有一个公共平台，那就是containerView，也就是导航控制器View
    //动画完成后，在最后一步(真正的最后一步)调用completeTransition，告诉系统动画完成了，动画完成了系统就要执行出栈入栈的操作了
    //completeTransition完成控制器的出栈入栈的操作，放在最后，，，它会自动定位让哪个控制器出栈入栈，所以fromVC和toVC的view在containerView的层级关系并不影响导航控制器调度它管理的控制器
    
    
    //注意pop和push时fromVC和toVC是不一样的
    
    //[transitionContext.containerView addSubview:toVC.view];
  
    
//    [UIView animateWithDuration:self.duration animations:^{
//        fromVC.view.transform=CGAffineTransformMakeTranslation(-320, 0);
//        //toVC.view.alpha=1.0;
//    } completion:^(BOOL finished) {
//        fromVC.view.transform=CGAffineTransformIdentity;
//        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
//    }];
//    
//    [UIView animateWithDuration:2.0 animations:^{
//        
//        toVC.view.alpha=1.0;
//    } completion:^(BOOL finished) {
//       
//        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
//    }];
    if(self.operation==UINavigationControllerOperationPush){
        [transitionContext.containerView insertSubview:toVC.view belowSubview:fromVC.view];
        fromVC.view.alpha=1.0;
        [UIView animateWithDuration:self.duration?self.duration:1.0 animations:^{
            fromVC.view.alpha=0.0;
        //fromVC.imageView.frame=toVC.view.bounds;
    } completion:^(BOOL finished) {
        //fromVC.imageView.frame=CGRectMake(100, 100, 100, 100);
        fromVC.view.alpha=1.0;
        
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
    }else if (self.operation==UINavigationControllerOperationPop){
       
        [transitionContext.containerView insertSubview:toVC.view belowSubview:fromVC.view];
       
        fromVC.view.alpha=1.0;

        
        [UIView animateWithDuration:self.duration?self.duration:1.0 animations:^{
            fromVC.view.transform=CGAffineTransformMakeScale(-0.15,-0.15);
            fromVC.view.alpha=0.0;
        } completion:^(BOOL finished) {
            fromVC.view.alpha=1.0;
            fromVC.view.transform=CGAffineTransformIdentity;
   
            
            
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
      
        }];

    
    
    
    
    }
    
    
    
   
}

@end
